import csv
import json
import os
from redis import Redis
from datetime import datetime

from itemadapter import ItemAdapter

from BooksSipder.items import InfoSpiderItem


class JsonPipeline:
    def __init__(self):
        self.Romance_file = None
        self.Info_file = None
        self.scrapy_name = None
        self.file_name = None
        self.file_start_time = datetime.now()
        self.Redis = Redis(host='localhost', port=6379, decode_responses=True)

    def process_item(self, item, spider):
        try:
            self.Info_file.write(
                json.dumps(ItemAdapter(InfoSpiderItem(item)).asdict(), ensure_ascii=False) + "," + "\n")
        except KeyError:
            self.Romance_file.write(json.dumps(ItemAdapter(item).asdict(), ensure_ascii=False) + "," + "\n")
        return item

    def open_spider(self, spider):
        self.scrapy_name = spider.name
        self.file_name = f"{self.scrapy_name}_{self.file_start_time}"
        if not os.path.exists("file/json/"):
            os.makedirs(f"file/json/")
        else:
            self.Romance_file = open(f'file/json/Roman_{self.file_name}.jl', 'w')
            self.Info_file = open(f'file/json/Info_file_{self.file_name}.jl', 'w')

    def close_spider(self, spider):
        self.Romance_file.close()
        self.Info_file.close()
